//: containers/ReadOnly.java
// Using the Collections.unmodifiable methods.
package containers;

import net.mindview.util.Countries;

import java.util.*;

import static net.mindview.util.Print.print;

public class ReadOnly {

    //生成国家类的数据 6个国家明细
    static Collection<String> data = new ArrayList<String>(Countries.names(6));

    public static void main(String[] args) {

        //为什么这么设计？？？并发模式的不变模式吗？
        Collection<String> c = Collections.unmodifiableCollection(new ArrayList<String>(data));
        print(c); // Reading is OK
//        c.add("one"); // Can't change it
//        at java.util.Collections$UnmodifiableCollection.add(Collections.java:1055)
//        at containers.ReadOnly.main(ReadOnly.java:21)

        List<String> a = Collections.unmodifiableList(new ArrayList<String>(data));
        ListIterator<String> lit = a.listIterator();
        print(lit.next()); // Reading is OK
        //! lit.add("one"); // Can't change it

        Set<String> s = Collections.unmodifiableSet(new HashSet<String>(data));
        print(s); // Reading is OK
        //! s.add("one"); // Can't change it

        // For a SortedSet:
        Set<String> ss = Collections.unmodifiableSortedSet(new TreeSet<String>(data));

        Map<String, String> m = Collections.unmodifiableMap(new HashMap<String, String>(Countries.capitals(6)));
        print(m); // Reading is OK
        //! m.put("Ralph", "Howdy!");

        // For a SortedMap:
        Map<String, String> sm = Collections.unmodifiableSortedMap(new TreeMap<String, String>(Countries.capitals(6)));
    }
} /* Output:
[ALGERIA, ANGOLA, BENIN, BOTSWANA, BULGARIA, BURKINA FASO]
ALGERIA
[BULGARIA, BURKINA FASO, BOTSWANA, BENIN, ANGOLA, ALGERIA]
{BULGARIA=Sofia, BURKINA FASO=Ouagadougou, BOTSWANA=Gaberone, BENIN=Porto-Novo, ANGOLA=Luanda, ALGERIA=Algiers}
*///:~
